//
//  LGFollowRecordItemView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/17.
//  录音item

#import "LGFollowRecordItemView.h"
#import <AVFoundation/AVFoundation.h>

@interface LGFollowRecordItemView()
/**   */
@property (nonatomic , strong)UIImageView *iconView;
/**   */
@property (nonatomic , strong)UILabel *secondLabel;

@end

@implementation LGFollowRecordItemView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.cornerRidus = 4;
        self.backgroundColor = [UIColor colorWithString:@"#0199FE"];
        [self addSubview:self.iconView];
        [self addSubview:self.secondLabel];
        [self addSubview:self.deleteBtn];
        [self addSubview:self.playBtn];
        [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(viewPix(10));
            make.centerY.equalTo(self);
            make.width.height.equalTo(@(viewPix(12)));
        }];
        [self.secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconView.mas_right).offset(viewPix(5));
            make.centerY.equalTo(self);
        }];
        [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self);
            make.width.equalTo(@(viewPix(32)));
        }];
        [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.equalTo(self);
            make.right.equalTo(self.deleteBtn.mas_left);
        }];
    }
    return self;
}

-(void)setCanDelete:(BOOL)canDelete{
    _canDelete = canDelete;
    self.deleteBtn.hidden = !canDelete;
    CGFloat width = (canDelete == YES)?viewPix(32):0;
    [self.deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(width));
    }];
}

-(void)setDuration:(NSString *)duration{
    _duration = duration;
    self.secondLabel.text = [NSString stringWithFormat:@"%@″",duration];
}

-(void)setAudioUrl:(NSString *)audioUrl{
    _audioUrl = audioUrl;
}

-(void)setPlaying:(BOOL)playing{
    _playing = playing;
    if(playing == YES){
        self.backgroundColor = [UIColor colorWithString:@"#CCE4FF"];
        self.iconView.image = [UIImage imageNamed:@"处理意见-暂停播放"];
        self.secondLabel.textColor = [UIColor colorWithString:@"#0199FE"];
    }else{
        self.backgroundColor = [UIColor colorWithString:@"#0199FE"];
        self.iconView.image = [UIImage imageNamed:@"处理意见-播放录音"];
        self.secondLabel.textColor = [UIColor colorWithString:@"#FFFFFF"];
    }
}

-(void)playAudioAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(playVoiceWithUrl:index:)]){
        [self.delegate playVoiceWithUrl:self.audioUrl index:self.index];
    }
}

-(UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"处理意见-播放录音"]];
    }
    return _iconView;
}

-(UILabel *)secondLabel{
    if(!_secondLabel){
        _secondLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _secondLabel;
}

-(UIButton *)deleteBtn{
    if(!_deleteBtn){
        _deleteBtn = [UIButton buttonWithImage:@"处理意见-删除录音" selectImage:@"处理意见-删除录音"];
    }
    return _deleteBtn;
}

-(UIButton *)playBtn{
    if(!_playBtn){
        _playBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_playBtn addTarget:self action:@selector(playAudioAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}

@end
